(function(win) {
  var getClasses = function(el) {
    return el.className.split(/\s+/g);
  };

  var addClass = function(el, name) {
    var classList = getClasses(el)
    if (classList.indexOf(name) == -1) {
      classList.push(name)
      el.className = classList.join(" ")
    }
  }

  var removeClass = function(el, name) {
    var classList = getClasses(el)
    classList[classList.indexOf(name)] = null
    el.className = classList.join(" ")
  }

  var toggleClass = function(el, name) {
    var classList = getClasses(el)
    if (classList.indexOf(name) == -1) {
      addClass(el, name)
      return true
    }
    else {
      removeClass(el, name)
      return false
    }
  }

  var html = document.getElementsByTagName("HTML")[0]

  var hideCode = localStorage.getItem("lsg-hide-code")

  if (hideCode == "true") {
    addClass(html, "lsg-hide-code")
  }

  function toggleCode(event) {
    event.preventDefault()
    var hideCode = toggleClass(html, "lsg-hide-code")
    localStorage.setItem("lsg-hide-code", hideCode)
  }

  document.addEventListener("DOMContentLoaded", function() {
    var switches = document.getElementsByClassName("lsg-code-switch")
    for (var i = 0; i < switches.length; i++) {
      switches[i].addEventListener("click", toggleCode)
    }
  })

})(window);
